package com.yf.listview;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ListView lv_list = (ListView) findViewById(R.id.lv_list1);
		lv_list.setAdapter(new MyAdapter());
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return 500;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// 注意:ListView的宽高必须为一个固定值或者match_parent，否则会导致额外调用很多次getView方法
			TextView tv_item = null;

			if (convertView == null) {
				// 当前没有可以重用的view，创建新的view
				tv_item = new TextView(MainActivity.this);
				Log.v("getView", "创建新的view对象" + position);
			} else {
				// 当前存在可复用的view, 直接转换成TextView
				tv_item = (TextView) convertView;
				Log.v("getView", "重用旧的view对象" + position);
			}
			
			tv_item.setText("我是第" + position + "条目");

			return tv_item;
		}

	}
}
